﻿/*
 * AppInfo
 * 
 * Defines basic info about application registered to run in XapExplorer
 * 
 * created by: asida@centrum.cz
 * 
 * initial implementation: 6.12.2011
 * last updated:
 */
using System;
//using System.Net;
using System.Windows;
//using System.Windows.Controls;
//using System.Windows.Documents;
//using System.Windows.Ink;
//using System.Windows.Input;
using System.Windows.Media;
//using System.Windows.Media.Animation;
//using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using System.Collections.Generic;


namespace uhul
{
  public class AppInfo : DependencyObject
  {


    public AppInfo()
      : base()
    { }


    /// <summary>
    /// get/set application name
    /// </summary>
    public string Name
    { get; set; }





    /// <summary>
    /// get/set application description
    /// </summary>
    public string Description
    { get; set; }






    /// <summary>
    /// type of application, some apps disappears when inactive
    /// </summary>
    public AppTypeEnum AppType
    { get; set; }





    /// <summary>
    /// get/set application visibility in menu. Default (not set) value is visible
    /// </summary>
    public Visibility MenuVisible
    { get; set; }

    //private Visibility _visibility;


    //public Visibility MenuVisible
    //{
    //  get 
    //  {
    //    if (_visibility == null)
    //      return Visibility.Visible;
    //    else
    //      return _visibility;
    //  }
    //  set
    //  {
    //    _visibility = value;
    //  }
    //}






    //public static readonly DependencyProperty ThemeColorProperty =
    //  DependencyProperty.Register("ThemeColor", typeof(object),
    //  typeof(AppInfo), new PropertyMetadata(null, new PropertyChangedCallback(onIconChanged)));

    /// <summary>
    /// Base Color used for application background on VCard, TabBtn.
    /// This color should correspondent also to app Background property.
    /// </summary>
    //public Color ThemeColor
    //{ get; set; }





    /// <summary>
    /// Base Color used for application background on VCard, TabBtn.
    /// This color should correspondent also to app Background property.
    /// </summary>
    public static readonly DependencyProperty ThemeColorProperty = DependencyProperty.Register(
        "ThemeColor", typeof(object), typeof(AppInfo), new PropertyMetadata(OnThemeColorPropertyChanged));

    /// <summary>
    /// Gets or sets a value indicating text displayed inside of map pin.  This is a dependency property.
    /// </summary>
    /// <value></value>
    /// <returns></returns>
    public SolidColorBrush ThemeColor
    {
      get
      {
        //object tmp = GetValue(ThemeColorProperty);

        //if (!(tmp is Color))
        //  return Colors.Gray;
        //else
        return (SolidColorBrush)GetValue(ThemeColorProperty);
      }
      set { SetValue(ThemeColorProperty, value); }
    }


    private static void OnThemeColorPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
      if (e.NewValue != null)
        if (!(e.NewValue is SolidColorBrush) && e.NewValue is String)
          if (((string)e.NewValue).Length == 9)
          {
            string s = e.NewValue as string;
            //try convert object to Color
            byte a = System.Convert.ToByte(s.Substring(0, 2), 16);
            byte r = System.Convert.ToByte(s.Substring(2, 2), 16);
            byte g = System.Convert.ToByte(s.Substring(4, 2), 16);
            byte b = System.Convert.ToByte(s.Substring(6, 2), 16);

            SolidColorBrush tmpCol = new SolidColorBrush(Color.FromArgb(a, r, g, b));
            //MapPinButton m = (MapPinButton)d;
            //m.GradientColor = tmp;

            AppInfo tmpAppInfo = (AppInfo)d;

            tmpAppInfo.ThemeColor = tmpCol;
          }

    }




    public static readonly DependencyProperty IconProperty =
      DependencyProperty.Register("Icon", typeof(object),
      typeof(AppInfo), new PropertyMetadata(null, new PropertyChangedCallback(onIconChanged)));

    /// <summary>
    /// get/set application icon to show in AppMenu
    /// </summary>
    public ImageSource Icon
    {
      get { return (ImageSource)GetValue(IconProperty); }
      set
      {
        SetValue(IconProperty, value);
      }
    }

    private static void onIconChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {

      if (e.NewValue != null)
        if (!(e.NewValue is ImageSource))
        {
          //try convert object to URI....
          ImageSource tmp = _getImageSource(e.NewValue);
          AppInfo a = (AppInfo)d;

          a.Icon = tmp;
        }
    }





    public static readonly DependencyProperty ProviderIconProperty =
      DependencyProperty.Register("ProviderIcon", typeof(object),
      typeof(AppInfo), new PropertyMetadata(null, new PropertyChangedCallback(onProviderIconChanged)));

    /// <summary>
    /// get/set application icon to show in AppMenu
    /// </summary>
    public ImageSource ProviderIcon
    {
      get { return (ImageSource)GetValue(ProviderIconProperty); }
      set
      {
        SetValue(ProviderIconProperty, value);
      }
    }

    private static void onProviderIconChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {

      if (e.NewValue != null)
        if (!(e.NewValue is ImageSource))
        {
          //try convert object to URI....
          ImageSource tmp = _getImageSource(e.NewValue);
          AppInfo a = (AppInfo)d;

          a.ProviderIcon = tmp;
        }
    }





    public static readonly DependencyProperty XapUriProperty =
      DependencyProperty.Register("XapUri", typeof(object),
      typeof(AppInfo), new PropertyMetadata(null, new PropertyChangedCallback(onXapUriChanged)));

    /// <summary>
    /// get/set <c>Uri</c> to application xap file location
    /// </summary>
    public Uri XapUri
    {
      get { return (Uri)GetValue(XapUriProperty); }
      set
      {
        SetValue(XapUriProperty, value);
      }
    }





    private static void onXapUriChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {

      if (e.NewValue != null)
        if (!(e.NewValue is Uri))
        {
          //try convert object to URI....
          Uri tmp = _getUri(e.NewValue);
          AppInfo a = (AppInfo)d;

          a.XapUri = tmp;
        }

    }





    private static Uri _getUri(object value)
    {
      //just for sure...
      if (value == null)
        return null;

      Uri sourceUri;

      if (value is Uri)
        sourceUri = (Uri)value;

      else if (value is string)
      {
        // Create sourceUri
        string stringValue = value as string;
        sourceUri = new Uri(stringValue, UriKind.RelativeOrAbsolute);
      }
      else
        //return null;
        throw new ArgumentException("not an Uri");

      return sourceUri;

      //// We got an abosulte Uri we do not need to modify it.
      //if (sourceUri.IsAbsoluteUri)
      //  return sourceUri;

      //return null;
    
    }




    private static ImageSource _getImageSource(object value)
    {
      //just for sure...
      if (value == null)
        return null;

      //must be some Uri.
      Uri tmp = _getUri(value);

      ImageSource i = new BitmapImage(tmp);
      if(i == null)
        throw new ArgumentException("not an image uri");

      return i;

    }



    /// <summary>
    /// get/set name of assembly (including file extension such as dll)
    /// </summary>
    public string AssemblyName
    { get; set; }





    /// <summary>
    /// get/set full name of user control that should be invoked from assembly
    /// </summary>
    public string UserControlName
    { get; set; }





    ///// <summary>
    ///// get/set extended properties of current application
    ///// </summary>
    //public Dictionary<string, string> WebServiceEntryPoints
    //{ get; set; }

    private static readonly DependencyProperty WebServiceEntryPointsProperty =
        DependencyProperty.Register(
          "WebServiceEntryPoints",
          typeof(Dictionary<string, string>),
          typeof(AppInfo),
          new PropertyMetadata(new Dictionary<string, string>())
        );

    public Dictionary<string, string> WebServiceEntryPoints
    {
      get { return (Dictionary<string, string>)GetValue(WebServiceEntryPointsProperty); }
      set { SetValue(WebServiceEntryPointsProperty, (Dictionary<string, string>)value); }
    }



  }

}
